//
//  TopicDetailViewController.m
//  lisure0.1
//
//  Created by xalo on 16/4/22.
//  Copyright © 2016年 Mr.wangYongxu. All rights reserved.
//

#import "TopicDetailViewController.h"
#import "TopicModel.h"
#import "CounterListModel.h"
#import "authOrUsrModel.h"
#import "TopicCommentModel.h"
#import "TopicHeadView.h"
#import "TopicCommentCell.h"
#import "TopicWebViewCell.h"

#define kTopicDetailURL @"http://api2.pianke.me/group/posts_info"

@interface TopicDetailViewController ()<UIWebViewDelegate, UITableViewDelegate, UITableViewDataSource>

@property(nonatomic, retain)NSMutableArray *modelDataArr;
@property(nonatomic, retain)CounterListModel* couterListInfoModel;
@property(nonatomic, retain)NSDictionary* shareInfoDic;
@property(nonatomic, retain)NSArray* imglistArr;
@property(nonatomic, retain)NSString* htmlStr;

@property(nonatomic, retain)TopicHeadView* headView;
@property(nonatomic, assign)CGFloat height;
@property(nonatomic, retain)TopicWebViewCell* cell;
@property (weak, nonatomic)IBOutlet UITableView* tableView;
@end

@implementation TopicDetailViewController


//-(TopicWebViewCell *)cell{
//
//    if (!_cell) {
//        _cell = [[TopicWebViewCell alloc] init];
//    }
//    return _cell;
//}
-(NSMutableArray *)modelDataArr{

    if (!_modelDataArr) {

        _modelDataArr = [NSMutableArray array];
    }
    return _modelDataArr;
}

-(CounterListModel *)couterListInfoModel{

    if (_couterListInfoModel) {
        _couterListInfoModel = [[CounterListModel alloc] init];
    }
    return _couterListInfoModel;
}

-(void)createHeadView{
    UINib *nib = [UINib nibWithNibName:@"TopicHeadView" bundle:nil];
    self.headView = [[nib instantiateWithOwner:nil options:nil] lastObject];
    [self.headView setViews:self.model];
    self.tableView.tableHeaderView = self.headView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
//    NSLog(@"%@",self.model);
    self.automaticallyAdjustsScrollViewInsets = YES;
    [self.view layoutIfNeeded];
    [self createHeadView];
    [self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    [self.tableView setSeparatorColor:[UIColor darkGrayColor]];
    [self.tableView registerNib:[UINib nibWithNibName:@"TopicCommentCell" bundle:nil] forCellReuseIdentifier:@"TopicCommentCell"];
    [self fetchdatawithUrl:kTopicDetailURL parameter:@{@"contentid":self.model.contentid}];
    [self.tableView registerNib:[UINib nibWithNibName:@"TopicWebViewCell" bundle:nil] forCellReuseIdentifier:@"TopicWebViewCell"];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reloadTableView:) name:@"height" object:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)reloadTableView:(NSNotification*)sender{

    self.height = [sender.userInfo[@"height"] floatValue];
    [self.tableView reloadData];

}
-(void)fetchdatawithUrl:(NSString*)url parameter:(NSDictionary*)dic{

    [MBProgressHUD showHUDAddedTo:self.headView animated:YES];
    AFHTTPSessionManager* manager = [AFHTTPSessionManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    [manager POST:url parameters:dic progress:^(NSProgress * _Nonnull uploadProgress) {

    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSLog(@"话题详情请求成功");
        NSDictionary* dataDic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingAllowFragments error:nil];
        NSDictionary* postsinfo = dataDic[@"data"];
        self.shareInfoDic = postsinfo[@"postsinfo"][@"shareinfo"];
        self.htmlStr = postsinfo[@"postsinfo"][@"html"];
        [self.couterListInfoModel setValuesForKeysWithDictionary:postsinfo[@"counterList"]];
        NSArray* array = postsinfo[@"commentlist"];
        for (int i =0; i < array.count; i++ ) {
            TopicCommentModel* model = [[TopicCommentModel alloc] init];
            [model setValuesForKeysWithDictionary:array[i]];
            [self.modelDataArr addObject:model];
        }
        [MBProgressHUD hideHUDForView:self.headView animated:YES];
        [self.tableView reloadData];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"话题详情请求失败%ld",error.code);
        [MBProgressHUD hideHUDForView:self.headView animated:YES];
    }];
}




#pragma mark - <UITableViewDelegate,UItableViewDataSource>
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    if (indexPath.row == 0) {
        TopicWebViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TopicWebViewCell" forIndexPath:indexPath];
        [cell.webView loadHTMLString:self.htmlStr baseURL:nil];
        return cell;
    }else{

    TopicCommentCell* cell = [tableView dequeueReusableCellWithIdentifier:@"TopicCommentCell" forIndexPath:indexPath];
    [cell setCellWithModel:self.modelDataArr[indexPath.row]];
        return cell;}
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [tableView showMessage:@"无评论" byDataSourceCount:self.modelDataArr.count];
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    if (indexPath.row == 0) {
        return self.height;
    }
    return 150;
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
